package com.example.sonny.util.adapter;

import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.sonny.bean.BookItem;
import com.example.sonny.dutest.R;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.net.URISyntaxException;
import java.util.ArrayList;

/**
 * Created by Sonny on 2015/8/26.
 */
public class BookListAdapter extends BaseAdapter {

    ArrayList<BookItem> books;
    LayoutInflater inflater;

    public BookListAdapter(Context context,ArrayList<BookItem> books) {

        this.books = books;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return books.size();
    }

    @Override
    public Object getItem(int position) {
        return books.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        BookItem book =  books.get(position);
        ViewHolder holder;
        if(convertView == null){
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.book_list_row,null);
            holder.name_tv = (TextView)convertView.findViewById(R.id.name_tv);
            holder.intr_tv = (TextView)convertView.findViewById(R.id.intr_tv);
            holder.thumb_iv = (ImageView)convertView.findViewById(R.id.thumb_iv);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder)convertView.getTag();
        }
        holder.name_tv.setText(book.getName());
        holder.intr_tv.setText(book.getIntro());
        ImageLoader imageLoader = ImageLoader.getInstance();
        imageLoader.displayImage(book.getImgUrl(),holder.thumb_iv);
        //holder.thumb_iv.setImageURI(Uri.parse(book.getImgUrl()));
        return convertView;
    }

    class ViewHolder{
        TextView name_tv;
        TextView intr_tv;
        ImageView thumb_iv;
    }
}
